package com.framework2.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.percent.PercentRelativeLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.framework.Utils.ScreenUtils;
import com.framework2.base.BaseOnClickListener;
import com.framework2.utils.PicToastUtil;
import com.asiainfo.heshanghui.R;

/**
 * 填写配件信息
 *
 *className InputAccessoriesPopupwindow
 */

public class InputAccessoriesPopupwindow extends PopupWindow {
    private Context context;
    private View viewRoot;
    private EditText et_input;
    private TextView tv_pop_title;
    private TextView tv_pop_content;
    private ImageView iv_delete;
    private TextView tv_cancel;
    private TextView tv_sure;

    public InputAccessoriesPopupwindow(Context context) {
        super(context);
        this.context = context;
        initView();
        initStyle();
        initEvent();
    }


    private void initView() {
        viewRoot = LayoutInflater.from(context).inflate(
                R.layout.popwindow_input_accessories, null);
        tv_pop_title = (TextView) viewRoot.findViewById(R.id.tv_pop_title);
        tv_pop_content = (TextView) viewRoot.findViewById(R.id.tv_pop_content);
        et_input = (EditText) viewRoot.findViewById(R.id.et_input);
        iv_delete = (ImageView) viewRoot.findViewById(R.id.iv_delete);
        tv_cancel = (TextView) viewRoot.findViewById(R.id.tv_cancel);
        tv_sure = (TextView) viewRoot.findViewById(R.id.tv_sure);
        setWidth(ScreenUtils.getInstance().getScreenWidthPx(context));
        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        int height=(int)((ScreenUtils.getInstance().getScreenHeightPx(context)- ScreenUtils.getInstance().getStatusBarHeightPx(context)- ScreenUtils.getInstance().dip2px(context,50))*0.40);
        PercentRelativeLayout.LayoutParams layoutParams=new PercentRelativeLayout.LayoutParams(ScreenUtils.getInstance().getScreenWidthPx(context)- ScreenUtils.getInstance().dip2px(context,40),height);
        viewRoot.findViewById(R.id.rootView).setLayoutParams(layoutParams);
        setContentView(viewRoot);
    }

    private void initStyle() {
        // 设置整个popupwindow的弹出，收起方式
        setAnimationStyle(R.style.popWindowAnim);
        // 需要设置一下此参数，点击外边可消失
        setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        setFocusable(true);
    }

    private String beforeTextChanged;

    private void initEvent() {
        iv_delete.setOnClickListener(new BaseOnClickListener() {
            @Override
            protected void onBaseClick(View v) {
                dismiss();
            }
        });
        tv_cancel.setOnClickListener(new BaseOnClickListener() {
            @Override
            public void onBaseClick(View v) {
                dismiss();
            }
        });
        tv_sure.setOnClickListener(new BaseOnClickListener() {
            @Override
            public void onBaseClick(View v) {
                if (TextUtils.isEmpty(et_input.getText())) {
                    PicToastUtil.showPicToast(context, "请输入入库数量!");
                    return;
                }
                if (null != onclickListener) {
                    onclickListener.onSureClickListener(et_input.getText().toString());
                }
            }
        });
    }

    public EditText getEditText() {
        return et_input;
    }

    public InputAccessoriesPopupwindow setTitle(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            tv_pop_title.setText(charSequence);
        return this;
    }

    public InputAccessoriesPopupwindow setHintTop(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            et_input.setHint(charSequence);
        return this;
    }


    public InputAccessoriesPopupwindow setContent(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            tv_pop_content.setText(charSequence);
        return this;
    }

    public InputAccessoriesPopupwindow setContentVisible(boolean visible) {
        tv_pop_content.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public InputAccessoriesPopupwindow setSureButtonText(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            tv_sure.setText(charSequence);
        return this;
    }

    public InputAccessoriesPopupwindow setEditTextHint(CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence))
            et_input.setHint(charSequence);
        return this;
    }

    private OnSureClickListener onclickListener;

    public InputAccessoriesPopupwindow setOnSureClickListener(OnSureClickListener onclickListener) {
        this.onclickListener = onclickListener;
        return this;
    }

    public interface OnSureClickListener {
        void onSureClickListener(String info1);
    }
}
